package com.humanmonth.home
{
	import com.humanmonth.home.component.page.contact.ContactAs;
	import com.humanmonth.home.component.page.him.HIMAs;
	import com.humanmonth.home.component.page.home.HomePage;
	import com.humanmonth.home.component.page.htree.HTreeAs;
	import com.humanmonth.home.component.page.msg.MsgAs;
	import com.humanmonth.home.component.page.portal.PortalAs;
	import com.humanmonth.home.component.page.report.MyReportAs;
	import com.humanmonth.home.component.page.signature.Signature;
	
	import flash.events.Event;
	import flash.ui.ContextMenu;
	
	import mx.containers.Canvas;
	import mx.core.Application;
	import mx.effects.AddChildAction;
	import mx.effects.Blur;
	import mx.effects.Fade;
	import mx.effects.Parallel;
	import mx.effects.RemoveChildAction;
	import mx.effects.Sequence;
	import mx.events.FlexEvent;
	import mx.states.RemoveChild;
	import mx.states.State;
	import mx.states.Transition;
	
	public class HomeAs extends Application
	{
		/**
		 *效果迟续时间 
		 */
		private var aTime:int=400;
			
		/**
		 *模糊度 
		 */
		private var aBlur:int=10;  
		
		/**
		 *页面体 
		 */
		public var body:Canvas;
		/**
		 *门户 
		 */
		public var portal:PortalAs=new PortalAs();
		/**
		 *树形图 
		 */
		public var hTree:HTreeAs=new HTreeAs();

		/**
		 *联系方式 
		 */
		public var contact:ContactAs=new ContactAs();
		/**
		 *首页
		 */
		public var homePage:HomePage=new HomePage();
		/**
		 *HIM 
		 */
		public var him:HIMAs=new HIMAs();
		/**
		 *留言 
		 */
		public var msg:MsgAs=new MsgAs();
		/**
		 *手写签名 
		 */
		public var signature:Signature=new Signature();
		/**
		 *报表 
		 */
		public var report:MyReportAs=new MyReportAs();
		public function HomeAs()
		{
			this.addEventListener(FlexEvent.INITIALIZE,myInit);
		}
		
		private function myInit(event:Event):void{
			var contextMenu:ContextMenu=new ContextMenu();
			contextMenu.hideBuiltInItems();
			this.contextMenu=contextMenu;
			this.initStates();
			this.initTranslates();
		}
		
		/**
		 *初始化状态 
		 */
		private function initStates():void{
			var stateHtree:State=new State();
			stateHtree.name="HTree";
			stateHtree.overrides=getRemoveAction();
			var statePortal:State=new State();
			statePortal.name="Portal";
			statePortal.overrides=getRemoveAction();
			var stateContact:State=new State();
			stateContact.name="Contact";
			stateContact.overrides=getRemoveAction();
			var hPage:State=new State();
			hPage.name="HPage";
			hPage.overrides=getRemoveAction();
			var shim:State=new State();
			shim.name="HIM";
			shim.overrides=getRemoveAction();
			var smsg:State=new State();
			smsg.name="msg";
			smsg.overrides=getRemoveAction();
			var signatureS:State=new State();
			signatureS.name="signature";
			signatureS.overrides=this.getRemoveAction();
			var reportS:State=new State();
			reportS.name="report";
			reportS.overrides=this.getRemoveAction();
			this.states=[stateHtree,statePortal,stateContact,hPage,shim,smsg,signatureS,reportS];
		}
		
		/**
		 * 移除动作 
		 */
		private function getRemoveAction():Array{
			return [new RemoveChild(this.portal),new RemoveChild(this.hTree),new RemoveChild(this.contact),new RemoveChild(this.homePage),new RemoveChild(this.him),new RemoveChild(this.msg),new RemoveChild(this.signature)];
		}
		
		/**
		 *初始化translates对像 
		 * 
		 */
		private function initTranslates():void{
			this.addTranslates("*","Contact",this.contact);
			this.addTranslates("*","HTree",this.hTree);
			this.addTranslates("*","Portal",this.portal);
			this.addTranslates("*","HPage",this.homePage);
			this.addTranslates("*","HIM",this.him);
			this.addTranslates("*","msg",this.msg);
			this.addTranslates("*","signature",this.signature);
			this.addTranslates("*","report",this.report);
		}
		
		/**
		 *增加translate 
		 * @param fromS 起始状态
		 * @param toS 结束状态
		 * @param obj 目标对像
		 * 
		 */
		private function addTranslates(fromS:String,toS:String,obj:Object):void{
		var transToContact:Transition=new Transition();
		transToContact.fromState=fromS;
		transToContact.toState=toS;
		transToContact.effect=this.getTransSequence(obj);
		this.transitions.push(transToContact);
		}
		/**
		 * 获取效果
		 */
		private function getTransSequence(obj:Object):Sequence{
		 //开始效果
		 var paralStart:Parallel=new Parallel();
		 var fadeStart:Fade=new Fade();
		 fadeStart.duration=this.aTime;
		 fadeStart.alphaFrom=1;
		 fadeStart.alphaTo=0;
		 var blurStart:Blur=new Blur();
		 blurStart.duration=this.aTime;
		 blurStart.blurXFrom=0;
		 blurStart.blurYFrom=0;
		 blurStart.blurXTo=this.aBlur;
		 blurStart.blurYTo=this.aBlur;
		 paralStart.addChild(fadeStart);
		 paralStart.addChild(blurStart);
		 //结束效果
		 var paralEnd:Parallel=new Parallel();
		 var fadeEnd:Fade=new Fade();
		 fadeEnd.duration=this.aTime;
		 fadeEnd.alphaFrom=0;
		 fadeEnd.alphaTo=1;
		 var blurEnd:Blur=new Blur();
		 blurEnd.duration=this.aTime;
		 blurEnd.blurXFrom=this.aBlur;
		 blurEnd.blurYFrom=this.aBlur;
		 blurEnd.blurXTo=0;
		 blurEnd.blurYTo=0;
		 paralEnd.addChild(fadeEnd);
		 paralEnd.addChild(blurEnd);	
		 //全局	 
		 var sequence:Sequence=new Sequence();
		 sequence.addChild(paralStart)
		 sequence.addChild(new RemoveChildAction(this.homePage));
		 sequence.addChild(new RemoveChildAction(this.contact));
		 sequence.addChild(new RemoveChildAction(this.portal));
		 sequence.addChild(new RemoveChildAction(this.hTree));
		 var add:AddChildAction=new AddChildAction(obj);
		 add.relativeTo=this.body;
		 sequence.addChild(add);
		 sequence.addChild(paralEnd);
		 sequence.target=this.body;
		 return sequence;
		}

	}
}